package com.driver.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONUtil;
import com.driver.system.controller.form.*;
import com.driver.system.db.pojo.DriverFineEntity;
import com.driver.system.service.DriverFineService;
import com.driver.system.service.DriverService;
import com.driver.system.util.PageUtils;
import com.driver.system.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 14375
 */
@RestController
@RequestMapping("/driver/fine")
@Tag(name = "DriverFineController", description = "司机罚款管理Web接口")
public class DriverFineController {
    @Resource
    private DriverFineService driverFineService;

    @PostMapping("/searchDriverFineByPage")
    @SaCheckPermission(value = {"ROOT", "DRIVERFINE:SELECT"}, mode = SaMode.OR)
    @Operation(summary = "查询司机罚款分页记录")
    public R searchDriverFineByPage(@RequestBody @Valid SearchDriverFineByPageForm form) {
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        Map param = BeanUtil.beanToMap(form);
        param.put("start", start);
        PageUtils pageUtils = driverFineService.searchDriverFineByPage(param);
        return R.ok().put("result", pageUtils);
    }

    @PostMapping("/searchDriverFineById")
    @Operation(summary = "根据ID查找罚款司机")
    @SaCheckPermission(value = {"ROOT", "DRIVERFINE:SELECT"}, mode = SaMode.OR)
    public R searchDriverFineById(@Valid @RequestBody SearchDriverFineByIdForm form) {
        HashMap map = driverFineService.searchDriverFineById(form.getFineId());
        return R.ok().put("result", map);
    }

    @PostMapping("/insertDriverFine")
    @Operation(summary = "添加司机罚款信息")
    public R insertDriverFine(@RequestBody @Valid InsertDriverFineForm form){
        DriverFineEntity driverFine = BeanUtil.toBean(form, DriverFineEntity.class);
        driverFine.setCreateTime(new DateTime());
        int rows = driverFineService.insertDriverFine(driverFine);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/updateDriverFine")
    @SaCheckPermission(value = {"ROOT", "DRIVERFINE:UPDATE"}, mode = SaMode.OR)
    @Operation(summary = "修改司机罚单信息")
    public R updateDriverFine(@Valid @RequestBody UpdateDriverFineForm form) {
        Map param = BeanUtil.beanToMap(form);
        int rows = driverFineService.updateDriverFine(param);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/deleteDriverFineByIds")
    @SaCheckPermission(value = {"ROOT", "DRIVERFINE:DELETE"}, mode = SaMode.OR)
    @Operation(summary = "批量删除司机罚款信息")
    public R deleteDriverFineByIds(@Valid @RequestBody DeleteDriverFineByIdsForm form) {
        int rows = driverFineService.deleteDriverFineByIds(form.getIds());
        return R.ok().put("rows", rows);
    }
}
